package com.lemon.smartsafety.ui.activity.address;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.AppCompatRadioButton;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.widget.Button;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.AppManager;
import com.lemon.smartsafety.app.AppService;
import com.lemon.smartsafety.app.Constants;
import com.lemon.smartsafety.app.EventTag;
import com.lemon.smartsafety.entity.event.address.AddressRefreshEvent;
import com.lemon.smartsafety.entity.event.http.HttpCallbackEvent;
import com.lemon.smartsafety.http.callback.HandlerCallback;
import com.lemon.smartsafety.http.service.AddressBookServices;
import com.lemon.smartsafetynew.ui.activity.base.SwipeBackActivity;
import com.lemon.smartsafety.utils.app.HUDUtil;
import com.lemon.smartsafety.utils.common.StringUtils;
import com.rengwuxian.materialedittext.MaterialEditText;

import org.greenrobot.eventbus.Subscribe;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Cooliean on 16/4/12.
 * 通讯录属性
 */
public class AddressBookPropActivity extends SwipeBackActivity {
    @Bind(R.id.metName)
    MaterialEditText     metName;
    @Bind(R.id.metKeyWord)
    MaterialEditText     metKeyWord;
    @Bind(R.id.radioYes)
    AppCompatRadioButton radioYes;
    @Bind(R.id.radioNo)
    AppCompatRadioButton radioNo;
    @Bind(R.id.metRemark)
    MaterialEditText     metRemark;
    @Bind(R.id.btn_submit)
    Button               btnSubmit;




    @Override
    protected void initTitle() {
        setBarTitle("通讯录");
    }

    @Override
    protected void callFinish() {
        AppManager.getAppManager().finishActivity(AddressBookPropActivity.this);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            callFinish();
        }
        return super.onKeyDown(keyCode, event);
    }
    @Override
    protected int getContentViewId() {
        return R.layout.activity_address_book_prop;
    }

    @Override
    protected void getBundleArgs() {
        super.getBundleArgs();
    }

    @Override
    protected void initViews() {
        super.initViews();
    }


    @OnClick({R.id.btn_submit})
    public void submitAction() {
        if (!validateForm()) {
            return;
        }
        getConfirmDialog("是否提交？", new MaterialDialog.SingleButtonCallback() {
            @Override
            public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                if (which == DialogAction.POSITIVE) {
                    doSubmitAction();
                }
            }
        }).show();
    }

    private boolean validateForm() {
        if (TextUtils.isEmpty(metName.getText().toString().trim())) {
            metName.setError("请填写通讯录名称");
            mHUDUtil.showErrorTip(metName.getError().toString());
            return false;
        }
        return true;
    }

    public void doSubmitAction() {
        String bookName = StringUtils.nullValue(metName.getText().toString().trim());
        //String keyWord = StringUtils.nullValue(metKeyWord.getText().toString().trim());
        //String isPublic = radioYes.isChecked() ? "1" : "0";
        String remark    = StringUtils.nullValue(metRemark.getText().toString().trim());
        String keyWord="";
        String isPublic="";

        AddressBookServices.init().saveAddressBook(bookName, keyWord, isPublic, remark);
    }

    /**
     * http处理请求的处理
     *
     * @param event
     */
    @Subscribe
    public void onEventMainThread(HttpCallbackEvent event) {
        if (EventTag.TAG_ADDRESS_BOOK_SAVE.equals(event.getTag())) {
            if (event.getHttpFlow() == HandlerCallback.HttpFlow.START) {
                mHUDUtil.showHUD();
            } else if (event.getHttpFlow() == HandlerCallback.HttpFlow.FINISH) {
                if (event.getEventResult() == Constants.Result.SUCCESS) {
                    mHUDUtil.showDoneTip(event.getMsg(), new HUDUtil.CallBack() {
                        @Override
                        public void onCall() {
                            AddressRefreshEvent refreshEvent = new AddressRefreshEvent();
                            AppService.getBus().post(refreshEvent);
                            callFinish();
                        }
                    });
                } else {
                    mHUDUtil.showErrorTip(event.getMsg());
                }
            }
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
